package com.servingcloud.servera.modules.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.servingcloud.servera.common.model.BasePagingQUERY;
import com.servingcloud.servera.common.model.RestPage;
import com.servingcloud.servera.modules.dao.UserInfoMapper;
import com.servingcloud.servera.modules.entity.UserInfo;
import com.servingcloud.servera.modules.model.CompanyBO;
import com.servingcloud.servera.modules.model.UserInfoMODIFY;
import com.servingcloud.servera.modules.model.UserInfoQUERY;
import com.servingcloud.servera.modules.model.UserInfoSAVE;
import com.servingcloud.servera.modules.service.UserInfoService;
import com.servingcloud.servera.utils.BeanCopierWrapper;
import com.servingcloud.servera.utils.IdGenerator;

@Service
public class UserInfoServiceImpl implements UserInfoService{

	@Autowired
	UserInfoMapper userInfoMapper;
	@Autowired
	IdGenerator idGenerator;//生成id
	public RestPage<UserInfo> findUserInfoPage(UserInfoQUERY query){
		PageHelper.startPage(query.getPageNo(), query.getPageSize());
		List<UserInfo> userList = userInfoMapper.findUserInfoPage(query);
		PageInfo<UserInfo> pageInfo = new PageInfo<>(userList);
		RestPage<UserInfo> page = new RestPage<UserInfo>();
		page.setTotal(pageInfo.getTotal());
		page.add(pageInfo.getList());
		return page;
	}
	@Override
	public Long saveUserInfo(UserInfoSAVE param) {
		UserInfo obj = new UserInfo();
		BeanCopierWrapper.copy(param, obj);
		obj.setUserId(idGenerator.nextId());
		userInfoMapper.insert(obj);
		return obj.getUserId();
	}
	@Override
	public void modifyUserInfo(UserInfoMODIFY param) {
		userInfoMapper.update(param);
	}
	@Override
	public void deleteUserInfo(UserInfoMODIFY param) {
		UserInfo obj = new UserInfo();
		BeanCopierWrapper.copy(param, obj);
		userInfoMapper.delete(obj);
	}
	@Override
	public RestPage<CompanyBO> findCompanyList() {
		String url = "http://localhost:8720/bapi/company/findCompanyPage";
		HttpHeaders headers = new HttpHeaders();
		//  请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		//  封装参数，千万不要替换为Map与HashMap，否则参数无法传递
		MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
		//  也支持中文
		params.add("pageNo", "1");
		params.add("pageSize", "20");
		//  执行HTTP请求
		RestTemplate restTemplate = new RestTemplate();
		
		ResponseEntity<RestPage> response = restTemplate.postForEntity(url, params, RestPage.class);
		//  输出结果
		return response.getBody();
	}
}